<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          @click="handleSearchList"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-row>
            <el-form-item label="订单编号：" style="min-width: 600px">
              <el-input
                v-model="listQuery.orderNum"
                class="input-width"
                placeholder="订单编号"
                style="width: 260px"
              ></el-input>
            </el-form-item>
            <el-form-item label="用户手机号：">
              <el-input
                v-model="listQuery.phone"
                class="input-width"
                placeholder="手机号"
                style="width: 200px"
              ></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item style="min-width: 600px" label="日期范围：">
              <el-date-picker
                v-model="searchDateRange"
                type="datetimerange"
                :picker-options="pickerOptions"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                align="right"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="订单状态：">
              <el-select
                v-model="listQuery.rightOrderStatusList"
                placeholder="请选择"
                clearable
                multiple
                style="width: 200px"
              >
                <el-option
                  v-for="item in rightOrderStatusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-row>
          <el-form-item label="供应商渠道：">
            <el-select
              v-model="listQuery.channel"
              placeholder="请选择"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in RIGHT_SUPPLIERS"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        size="mini"
        class="btn-add"
        @click="handleAddSku"
        style="margin-left: 20px"
        >添加</el-button
      >
    </el-card>
    <div class="table-container">
      <el-table
        ref="roleTable"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="权益类型" align="center" prop="benefitCode">
          <template slot-scope="scope">
            {{ benefitTypes.getValue(scope.row.benefitCode) }}
          </template>
        </el-table-column>
        <el-table-column label="订单编号" align="center" prop="orderNum">
        </el-table-column>
        <el-table-column label="用户手机号" align="center" prop="phone">
        </el-table-column>
        <el-table-column label="总金额（元）" align="center" prop="totalAmount">
          <template slot-scope="scope">
            {{ scope.row.totalAmount | formatCentToYuan }}
          </template>
        </el-table-column>
        <el-table-column label="应付金额（元）" align="center" prop="payAmount">
          <template slot-scope="scope">
            {{ scope.row.payAmount | formatCentToYuan }}
          </template>
        </el-table-column>
        <el-table-column label="订单状态" align="center" prop="status">
          <template slot-scope="scope">
            <el-tag
              :type="rightOrderStatusTag[scope.row.status].type"
              :color="rightOrderStatusTag[scope.row.status].color"
              >{{ rightOrderStatus.getValue(scope.row.status) }}</el-tag
            >
          </template>
        </el-table-column>

        <el-table-column label="支付方式" align="center" prop="payType">
          <template slot-scope="scope">{{
            payTypes.getValue(scope.row.payType)
          }}</template>
        </el-table-column>

        <el-table-column label="渠道" align="center" prop="channelName">
        </el-table-column>

        <el-table-column label="创建时间" align="center">
          <template slot-scope="{ row }">{{
            row.createdAt | formatDateTime
          }}</template>
        </el-table-column>

        <el-table-column label="更新时间" align="center">
          <template slot-scope="{ row }">{{
            row.updatedAt | formatDateTime
          }}</template>
        </el-table-column>

        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-row>
              <router-link
                :to="{
                  name: 'smsRightOrderDetail',
                  query: {
                    id: scope.row.orderNum,
                  },
                }"
                tag="a"
              >
                <el-button size="mini" type="text">查看详情</el-button>
              </router-link>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="PAGE_SIZES"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { formatDate } from "@/utils/date";
import ModalForm from "@/components/ModalForm.vue";
import { PAGE_SIZES, DEFAULT_PAGE_SIZE } from "@/utils/constant/system";
import { fetchRightOrderList } from "@/api/sms";
import {
  benefitTypes,
  payTypes,
  rightOrderStatus,
  rightOrderStatusTag,
} from "@/utils/constant/sms";
import { formatCentToYuan } from "@/utils/format";
import SelectScroll from "@/components/SelectScroll.vue";

const defaultListQuery = {
  pageNum: 1,
  pageSize: DEFAULT_PAGE_SIZE,
  startDate: "",
  endDate: "",
  rightOrderStatusList: [],
  orderNum: "",
  phone: "",
  channel: undefined,
};

const defaultRole = {
  id: null,
  name: null,
  description: null,
  adminCount: 0,
  status: 1,
};

const RIGHT_SUPPLIERS = Object.freeze([
  {
    value: "HEYE",
    label: "荷叶",
  },
  {
    value: "YIFENG",
    label: "益丰",
  },
]);

export default {
  name: "smsRightOrder",
  components: {
    SelectScroll,
    ModalForm,
  },
  data() {
    return {
      benefitTypes,
      rightOrderStatus,
      rightOrderStatusTag,
      payTypes,
      PAGE_SIZES,
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: false,
      isEdit: false,
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      searchDateRange: [],
      RIGHT_SUPPLIERS,
    };
  },
  created() {
    this.getList();
  },
  computed: {
    rightOrderStatusOptions() {
      return rightOrderStatus.toKeyValueArray();
    },
  },
  filters: {
    formatDateTime(time) {
      if (!time) {
        return "";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    },
    formatCentToYuan,
  },
  watch: {
    searchDateRange(val = []) {
      const [start, end] = val || [];

      this.listQuery.startDate = start;
      this.listQuery.endDate = end;
    },
  },
  methods: {
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
      this.searchDateRange = [];
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleAddSku() {
      this.$router.push({
        name: "amsAddActivity",
      });
    },

    getList() {
      this.listLoading = true;
      fetchRightOrderList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
  },
};
</script>
<style></style>
